package com.cskaoyan.service.index;

import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bean.*;
import com.cskaoyan.model.bean.advertise.Advertise;
import com.cskaoyan.model.bean.advertise.AdvertiseExample;
import com.cskaoyan.model.vo.index.HomeIndexVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：Chunk
 * @Description:
 * @Date Created in 16:21 2021/4/13
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    AdvertiseMapper advertiseMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    TopicMapper topicMapper;
    @Override
    public HomeIndexVO index() {
        //      创建用于存放最后结果的HomeIndexVo
        HomeIndexVO homeIndexVO = new HomeIndexVO();

//     newGoodsListBean 新货list
//        创建用于存放的list
        ArrayList<HomeIndexVO.NewGoodsListBean> newGoodsList = new ArrayList<>();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        for (Goods good : goods) {
            HomeIndexVO.NewGoodsListBean newGoodsListBean = new HomeIndexVO.NewGoodsListBean();
            newGoodsListBean.setId(good.getId());
            newGoodsListBean.setName(good.getName());
            newGoodsListBean.setBrief(good.getBrief());
            newGoodsListBean.setPicUrl(good.getPicUrl());
            newGoodsListBean.setIsNew(good.getIsNew());
            newGoodsListBean.setIsHot(good.getIsHot());
            newGoodsListBean.setCounterPrice(good.getBrandId());
            newGoodsListBean.setRetailPrice(good.getRetailPrice().intValue());
            newGoodsList.add(newGoodsListBean);
        }
        homeIndexVO.setNewGoodsList(newGoodsList);

//        存放coupon优惠券的list
        ArrayList<HomeIndexVO.CouponListBean> couponList = new ArrayList<>();
        CouponExample couponExample = new CouponExample();
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        for (Coupon coupon : coupons) {
            HomeIndexVO.CouponListBean couponListBean = new HomeIndexVO.CouponListBean();
            couponListBean.setId(coupon.getId());
            couponListBean.setName(coupon.getName());
            couponListBean.setDesc(coupon.getDesc());
            couponListBean.setTag(coupon.getTag());
            couponListBean.setDiscount(coupon.getDiscount().intValue());
            couponListBean.setMin(coupon.getMin().intValue());
            couponListBean.setDays(coupon.getDays());
            couponList.add(couponListBean);
        }
        homeIndexVO.setCouponList(couponList);

//      存放一級商品類目的channel
        ArrayList<HomeIndexVO.ChannelBean> channel = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria1 = categoryExample.createCriteria();
        criteria1.andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            HomeIndexVO.ChannelBean channelBean = new HomeIndexVO.ChannelBean();
            channelBean.setId(category.getId());
            channelBean.setName(category.getName());
            channelBean.setIconUrl(category.getIconUrl());
            channel.add(channelBean);
        }
        homeIndexVO.setChannel(channel);

//        团购部分的忽略

//        banner部分的内容
        ArrayList<HomeIndexVO.BannerBean> banner = new ArrayList<>();
        AdvertiseExample advertiseExample = new AdvertiseExample();
        List<Advertise> advertises = advertiseMapper.selectByExample(advertiseExample);
        for (Advertise advertise : advertises) {
            HomeIndexVO.BannerBean bannerBean = new HomeIndexVO.BannerBean();
            bannerBean.setId(advertise.getId());
            bannerBean.setName(advertise.getName());
            bannerBean.setLink(advertise.getLink());
            bannerBean.setUrl(advertise.getUrl());
            bannerBean.setPosition(advertise.getPosition());
            bannerBean.setContent(advertise.getContent());
            bannerBean.setEnabled(advertise.getEnabled());
            bannerBean.setAddTime(advertise.getContent());
            bannerBean.setUpdateTime(advertise.getContent());
            bannerBean.setDeleted(advertise.getDeleted());
            banner.add(bannerBean);
        }
        homeIndexVO.setBanner(banner);

//        bannerList的内容
        ArrayList<HomeIndexVO.BrandListBean> brandList = new ArrayList<>();
        BrandExample brandExample = new BrandExample();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        for (int i = 0; i < 4; i++) {
            HomeIndexVO.BrandListBean brandListBean = new HomeIndexVO.BrandListBean();
            brandListBean.setId(brands.get(i).getId());
            brandListBean.setName(brands.get(i).getName());
            brandListBean.setDesc(brands.get(i).getDesc());
            brandListBean.setPicUrl(brands.get(i).getPicUrl());
            brandListBean.setFloorPrice(Double.valueOf(brands.get(i).getFloorPrice()));
            brandList.add(brandListBean);
        }
        homeIndexVO.setBrandList(brandList);

//      hotgoodlist
        ArrayList<HomeIndexVO.HotGoodsListBean> hotGoodsList = new ArrayList<>();
        GoodsExample goodsExample1 = new GoodsExample();
        GoodsExample.Criteria criteria2 = goodsExample1.createCriteria();
        criteria2.andIsHotEqualTo(true);
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);

        for (Goods good : goods1) {
            HomeIndexVO.HotGoodsListBean hotGoodsListBean = new HomeIndexVO.HotGoodsListBean();
            hotGoodsListBean.setId(good.getId());
            hotGoodsListBean.setName(good.getName());
            hotGoodsListBean.setBrief(good.getBrief());
            hotGoodsListBean.setPicUrl(good.getPicUrl());
            hotGoodsListBean.setIsNew(good.getIsNew());
            hotGoodsListBean.setIsHot(good.getIsHot());
            hotGoodsListBean.setCounterPrice(good.getBrandId());
            hotGoodsListBean.setRetailPrice(good.getRetailPrice().intValue());
            hotGoodsList.add(hotGoodsListBean);
        }
        homeIndexVO.setHotGoodsList(hotGoodsList);

//      topicList 传入一个参数
        ArrayList<HomeIndexVO.TopicListBean> topicList = new ArrayList<>();
        TopicExample topicExample = new TopicExample();
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        for (int i = 0; i < 4; i++) {
            HomeIndexVO.TopicListBean topicListBean = new HomeIndexVO.TopicListBean();
            topicListBean.setId(topics.get(i).getId());
            topicListBean.setTitle(topics.get(i).getTitle());
            topicListBean.setSubtitle(topics.get(i).getSubtitle());
            topicListBean.setPrice(topics.get(i).getPrice().intValue());
            topicListBean.setReadCount(topics.get(i).getReadCount());
            topicListBean.setPicUrl(topics.get(i).getPicUrl());
            topicList.add(topicListBean);
        }
        homeIndexVO.setTopicList(topicList);

//        floorGoodsList的实现
        ArrayList<HomeIndexVO.FloorGoodsListBean> floorGoodsList = new ArrayList<>();
        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample.Criteria criteria3 = categoryExample1.createCriteria();
        criteria3.andLevelEqualTo("L1");
        List<Category> categories1 = categoryMapper.selectByExample(categoryExample1);
        for (Category category : categories1) {
            HomeIndexVO.FloorGoodsListBean floorGoodsListBean = new HomeIndexVO.FloorGoodsListBean();
            floorGoodsListBean.setName(category.getName());
            floorGoodsListBean.setId(category.getId());
//            往goodsList里面加入
            List<HomeIndexVO.FloorGoodsListBean.goodsList> goodsList = floorGoodsListBean.getGoodsList();
            GoodsExample goodsExample2 = new GoodsExample();
            goodsExample2.createCriteria().andCategoryIdEqualTo(category.getId());
            List<Goods> goods2 = goodsMapper.selectByExample(goodsExample2);
            for (Goods goods3 : goods2) {
                HomeIndexVO.FloorGoodsListBean.goodsList goodsList1 = new HomeIndexVO.FloorGoodsListBean.goodsList();
                goodsList1.setName(goods3.getName());
                goodsList1.setId(goods3.getId());
                goodsList1.setBrief(goods3.getBrief());
                goodsList1.setPicUrl(goods3.getPicUrl());
                goodsList1.setNew(goods3.getIsNew());
                goodsList1.setHot(goods3.getIsHot());
                goodsList1.setCounterPrice(goods3.getBrandId());
                goodsList1.setRetailPrice(goods3.getRetailPrice().intValue());
                goodsList.add(goodsList1);
            }

            floorGoodsListBean.setGoodsList(goodsList);
//            查询L2成功,就放入
            floorGoodsList.add(floorGoodsListBean);
        }
        homeIndexVO.setFloorGoodsList(floorGoodsList);

        return homeIndexVO;
    }
}
